package com.cloud.service.Impl;

import com.cloud.dao.ApplyVolunteerDao;
import com.cloud.domain.LoginUser;
import com.cloud.enumeration.ApplyState;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.UserApplyVolunteer;
import com.cloud.pojo.UserApplyVolunteerVue;
import com.cloud.service.ApplyVolunteerService;
import com.cloud.pojo.ApplyVolunteer;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import com.cloud.utils.MyTime;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： linJianHong
 * @dateTime： 14:47 2022/4/18
 * @Description:
 * @ClassName:
 */
@Service
public class ApplyVolunteerServiceImpl implements ApplyVolunteerService {

    @Resource
    ApplyVolunteerDao applyVolunteerDao;

    private final Factory resultFactory = new CommonResultFactory();

    @Override
    public CommonResult create(Map<String,Object> map) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = loginUser.getUser().getUserId();
        int volunteerLocation = (int)map.get("volunteerLocation");
        String volunteerTime = (String)map.get("volunteerTime");
        String state = ApplyState.UNDETERMINED;

        ApplyVolunteer applyVolunteer = new ApplyVolunteer();
        applyVolunteer.setUserId(userId);
        applyVolunteer.setVolunteerLocation(volunteerLocation);
        applyVolunteer.setVolunteerTime(MyTime.stringToDate(volunteerTime));
        applyVolunteer.setState(state);
        applyVolunteer.setApplyTime(MyTime.getCurrTimeAsDate());

        int res = applyVolunteerDao.create(applyVolunteer);
        if(res == 1){
            return resultFactory.getResultInstance(200,"志愿申请成功",1);
        }
        return resultFactory.getResultInstance(200,"志愿申请失败，请稍后重试",0);
    }

    @Override
    public CommonResult userUpdate(Map<String,Object> map) {
        int id = (int)map.get("id");
        int volunteerLocation = (int)map.get("volunteerLocation");
        String volunteerTime = (String)map.get("volunteerTime");
        Date date =  MyTime.stringToDate(volunteerTime);

        int res = applyVolunteerDao.userUpdate(id, volunteerLocation, date);
        if(res == 1){
            return resultFactory.getResultInstance(200,"志愿申请修改成功",1);
        }
        return resultFactory.getResultInstance(200,"志愿申请修改失败，请稍后重试",0);
    }


    @Override
    public CommonResult delete(Map<String,Object> map) {
        int id = (int)map.get("id");
        int res = applyVolunteerDao.delete(id);
        if(res == 1){
            return resultFactory.getResultInstance(200,"志愿申请撤回成功",1);
        }
        return resultFactory.getResultInstance(200,"志愿申请撤回失败，请稍后重试",0);
    }

    @Override
    public CommonResult userRetrieve() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = loginUser.getUser().getUserId();
        List<UserApplyVolunteer> res1 = applyVolunteerDao.userRetrieve(userId);
        List<UserApplyVolunteerVue> res2 = new ArrayList<>();
        for(UserApplyVolunteer item: res1){
            UserApplyVolunteerVue userApplyVolunteerVue = new UserApplyVolunteerVue();
            userApplyVolunteerVue.setId(item.getId());
            userApplyVolunteerVue.setApplyTime(MyTime.dateTime2String(item.getApplyTime()));
            userApplyVolunteerVue.setName(item.getName());
            userApplyVolunteerVue.setState(item.getState());
            userApplyVolunteerVue.setVolunteerTime(MyTime.date2String(item.getVolunteerTime()));
            userApplyVolunteerVue.setVolunteerLocation(item.getVolunteerLocation());
            res2.add(userApplyVolunteerVue);
        }
        if(res2.size() >= 1){
            return resultFactory.getResultInstance(200,"志愿申请查看成功",res2);
        }
        return resultFactory.getResultInstance(200,"暂无志愿申请记录",null);
    }


    @Override
    public CommonResult approvalRetrieve() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int defaultRegion = loginUser.getUser().getDefaultRegion();
        List<UserApplyVolunteer> res1 = applyVolunteerDao.approvalRetrieve(defaultRegion);
        List<UserApplyVolunteerVue> res2 = new ArrayList<>();
        for(UserApplyVolunteer item: res1){
            UserApplyVolunteerVue userApplyVolunteerVue = new UserApplyVolunteerVue();
            userApplyVolunteerVue.setId(item.getId());
            userApplyVolunteerVue.setUserId(item.getUserId());
            userApplyVolunteerVue.setApplyTime(MyTime.dateTime2String(item.getApplyTime()));
            userApplyVolunteerVue.setName(item.getName());
            userApplyVolunteerVue.setState(item.getState());
            userApplyVolunteerVue.setVolunteerTime(MyTime.date2String(item.getVolunteerTime()));
            userApplyVolunteerVue.setVolunteerLocation(item.getVolunteerLocation());
            res2.add(userApplyVolunteerVue);
        }
        if(res2.size() >= 1){
            return resultFactory.getResultInstance(200,"志愿申请查看成功",res2);
        }
        return resultFactory.getResultInstance(200,"暂无志愿申请记录",null);
    }

    @Override
    public CommonResult approvalUpdate(Map<String,Object> map) {
        int res = 0;
        List<Map<String, Object>> handles = (List<Map<String, Object>>)map.get("handleApplications");
        for(Map<String, Object> handle: handles){
            int id = (int)handle.get("id");
            String state = (String)handle.get("state");
            res += applyVolunteerDao.approvalUpdate(id, state);
            if(state.equals("同意")){
                String userId = (String)handle.get("userId");
                applyVolunteerDao.setUserTypeToVolunteer(userId);
            }
        }
        if(res == handles.size()){
            return resultFactory.getResultInstance(200,"志愿申请处理成功",res);
        }
        return resultFactory.getResultInstance(200,"志愿申请处理失败，请稍后重试",0);
    }
}
